﻿using System;
using System.Net;
using System.Windows.Forms;
using Core;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client; 

namespace TFSTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string domain = "tech";
            string tfsServer = @"http://192.168.0.230:8080";
            //string userName = "ccnet";
            //string password = "bitauto123!";
            string userName = "zhoujb";
            string password = "asdf1234!";
            string localPath = @"E:\TFS\B2SC\CommonLib";
            string tfsPath = @"$/B2SC/CommonLib";
            using (IdentityScope identity = new IdentityScope(userName, domain, password))
            {
                #region TFS模拟身份验证，未成功，所以使用IdentityScope模拟
                //NetworkCredential credentials = (string.IsNullOrEmpty(domain) ? new NetworkCredential(userName, password) : new NetworkCredential(userName, password, domain));
                //ICredentialsProvider credentialsProvider = new CredentialsProvider(credentials);
                //TfsTeamProjectCollection tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsServer), credentialsProvider);
                //Workspace workspace = versionControl.TryGetWorkspace(@"c:\test");
                //Workspace workspace = versionControl.CreateWorkspace("BasicSccExample", versionControl.AuthorizedUser);
                //Workspace workspace = versionControl.CreateWorkspace("BasicSccExample1", userName);
                #endregion
                TfsTeamProjectCollection tfs = TfsTeamProjectCollectionFactory.GetTeamProjectCollection(new Uri(tfsServer));
                VersionControlServer versionControl = (VersionControlServer)tfs.GetService(typeof(VersionControlServer));
                Workspace workspace = versionControl.TryGetWorkspace(localPath);
                if (workspace == null)
                {
                    workspace = versionControl.CreateWorkspace("BasicSccExample", versionControl.AuthorizedUser);
                    workspace.Map(tfsPath, localPath);
                }
                else
                {
                    if (!workspace.IsServerPathMapped(tfsPath))
                    {
                        workspace.Map(tfsPath, localPath);
                    }
                    //workspace.GetServerItemForLocalItem(localPath);
                    //workspace.Get在没有设置一对一的工作目录的情况下会获取所有代码，研究中。。。。。
                }
                workspace.Get(new GetRequest[] { new GetRequest(null, VersionSpec.Latest) }, 
                    GetOptions.Overwrite, 
                    (workspace1, operations, userData) => 
                        {
                            foreach (var item in operations)
                            {
                                Item i = versionControl.GetItem(item.SourceLocalItem);
                                DateTime dt = i.CheckinDate;
                                if (item.VersionLocal == item.VersionServer)
                                {
                                    //item.Ignore = true;
                                }
                            }
                        }, 
                        null);
                //workspace.PendAdd(localPath + "\\test.txt");
                //int editCount = workspace.PendEdit(localPath + "\\test.txt");
                
                //PendingChange[] pendingChanges = workspace.GetPendingChanges();
                //workspace.CheckIn(pendingChanges, "ccnet commend");
            }
        }

        public class CredentialsProvider : ICredentialsProvider
        {
            private readonly NetworkCredential credentials;

            public CredentialsProvider(NetworkCredential credentials)
            {
                this.credentials = credentials;
            }

            #region ICredentialsProvider Members

            public ICredentials GetCredentials(Uri uri, ICredentials failedCredentials)
            {
                return this.credentials;
            }

            public void NotifyCredentialsAuthenticated(Uri uri)
            {
            }

            #endregion
        }
    }
}
